//
//  WorkHomeVC.m
//  XLDoctor
//
//  Created by longma on 2018/7/27.
//  Copyright © 2018年 XLH. All rights reserved.
//mj邮箱注册

#import "RegisterEmailVC.h"
#import "LanguageController.h"
#import "CountryCodeVC.h"
#import "AllCountryCodeVC.h"
#import "NBPhoneNumberUtil.h"
#import "CaptchaView.h"
#import "EnterCodeController.h"
#import "RegisterViewController.h"

@interface RegisterEmailVC ()
@property (weak, nonatomic) IBOutlet UILabel *tipLabel;
@property (weak, nonatomic) IBOutlet MJUITextField *phoneNumberTextField;
@property (weak, nonatomic) IBOutlet MJUIBaseButton *confirmButton;
@property (weak, nonatomic) IBOutlet UIView *phoneNumberBgView;
@property (weak, nonatomic) IBOutlet UILabel *loginLabel;
@property (nonatomic, assign) BOOL isRegister;
@end

@implementation RegisterEmailVC

- (void)viewDidLoad {
    [super viewDidLoad];
   
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

}

- (void)mj_setUI{
    [super mj_setUI];
    
    [self.navigationController setNavigationBarHidden:true animated:NO];
    self.phoneNumberTextField.font = MJUIFontMake(20);
    
    self.tipLabel.hidden = true;
    [self.phoneNumberTextField mj_setClearButtonWithImage:[UIImage imageNamed:@"icon_del"]];

    if(self.emailString.length){
        self.phoneNumberTextField.text = self.emailString;
    }else{
        [self.phoneNumberTextField mj_becomeFirstResponder];
    }
    MJWeakSelf
    RAC(self.confirmButton, mj_enabled) = [RACSignal combineLatest:@[self.phoneNumberTextField.rac_textSignal] reduce:^id _Nullable(NSString * text){
        
        if(text.length > 0 && [text mj_validateEmail]){
            weakSelf.tipLabel.hidden = true;

            return @(true);
        }
        return @(false);
    }];

}

#pragma mark - 方法
- (void)requestData{
    
}

- (IBAction)handleLanguageButtonClick:(UIButton *)sender {
    LanguageController *vc = [[LanguageController alloc]init];
    [self.navigationController pushViewController:vc animated:true];
}

- (IBAction)handleRegisterButtonClick:(UIButton *)sender {
    [self.navigationController popToRootViewControllerAnimated:true];
}

- (IBAction)handlePhoneRegisterButtonClick:(UIButton *)sender {

    if([self.navigationController mj_containTargetVCWithClass:RegisterViewController.class]){
        [self mj_popViewController];
    }else{
        RegisterViewController *vc = [[RegisterViewController alloc]init];
        [self.navigationController pushViewController:vc animated:true];
    }
}


- (IBAction)handleConfirmButtonClick:(UIButton *)sender {
    [self.phoneNumberTextField resignFirstResponder];
    
    NSDictionary *params =  @{
        @"email": self.phoneNumberTextField.text,
    };
    MJWeakSelf
    [MJHttpNetWorking requestWithParams:params requestUri:user_findByEmail requestType:RequestTypeGet showLoading:true success:^(MJBaseModel *model) {
        
        if(model.code == 200){
            if(model.data){
                weakSelf.isRegister = true;
            }else{
                weakSelf.isRegister = false;
            }
            
            MJUIAlertViewConfigure *configure = [[MJUIAlertViewConfigure alloc]init];
            configure.confirmButtonColor = UIColor.mj_appColor;
            configure.confirmTitle = MJUILocalizedString(@"OK_qd");
            configure.cancelTitle = MJUILocalizedString(@"Cancel_qx");
            configure.messageAlignmentCenter = true;
            NSString *messageString = [[MJUILocalizedString(@"We will send a verification code to the following email address:") stringByAppendingString:@"\n"] stringByAppendingString:self.phoneNumberTextField.text];
            [MJUIAlertView alertViewWithTitle:MJUILocalizedString(@"Email Address Confirmation") message:messageString configure:configure confirmBlock:^{
                
                [weakSelf sendAnjCcaptchaCode];

            } cancelBlock:^{

            }];
        }

    } failure:^(NSError *error) {

    }];
    
    
    
}

/// 滑块验证码
- (void)sendAnjCcaptchaCode {
    
    MJWeakSelf
    [CaptchaView showWithType: puzzle CompleteBlock:^(NSString *  result) {
        NSLog(@"result=======================%@",result);
        NSMutableDictionary *parms = [NSMutableDictionary dictionaryWithDictionary:@{
            @"email": self.phoneNumberTextField.text,
            @"code": result,
        }];
        [MJHttpNetWorking requestWithParams:parms requestUri:captcha_sendEmail requestType:RequestTypePost showLoading:true success:^(MJBaseModel *model) {
            
            if (model.code ==  200){
                EnterCodeController *vc = [[EnterCodeController alloc]init];
                vc.captchaCodeType = weakSelf.isRegister ? CaptchaCodeTypeEmailLogin : CaptchaCodeTypeEmailRegister;
                vc.email = weakSelf.phoneNumberTextField.text;
                [weakSelf.navigationController pushViewController:vc animated:true];
            }else if(model.code ==  412){
                [RoyHud showToast:MJUILocalizedString(@"Too frequent, Retry later.")];
            }
            
        } failure:^(NSError *error) {
            
        }];
    }];
}

#pragma mark - 代理

#pragma mark - 懒加载

@end
